package guojin.config;

import guojin.pojo.Configeration;
import guojin.pojo.MappedStatement;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.xml.parsers.SAXParser;
import java.io.InputStream;
import java.util.List;

/**
 * @author haoge
 * @version $Id XMLMapperBuilder.java, v 0.1 2021-06-12 20:22 haoge Exp $$
 */
public class XMLMapperBuilder {

    private Configeration configeration;

    public  XMLMapperBuilder(Configeration configeration){
        this.configeration=configeration;
    }

    public  void  paresMapper(InputStream inputStream) throws DocumentException {

        Document mapperDocument = new SAXReader().read(inputStream);
        Element rootElement = mapperDocument.getRootElement();
        String namespace = rootElement.attributeValue("namespace");

        List<Element> list = rootElement.selectNodes("//select");
        //获取节点insert
        List<Element> insertList = rootElement.selectNodes("//insert");
        List<Element> deleteList = rootElement.selectNodes("//delete");
        List<Element> updateList = rootElement.selectNodes("//update");
        list.addAll(insertList);
        list.addAll(deleteList);
        list.addAll(updateList);
        for (Element element : list) {
            String id = element.attributeValue("id");
            String resultType = element.attributeValue("resultType");
            String paramterType = element.attributeValue("paramterType");
            String Sqltext = element.getTextTrim();
            MappedStatement mappedStatement = new MappedStatement();
            mappedStatement.setId(id);
            mappedStatement.setParamterType(paramterType);
            mappedStatement.setResultType(resultType);
            mappedStatement.setSql(Sqltext);
            String Key=namespace+"."+id;
            configeration.getMappedStatementMap().put(Key,mappedStatement);
        }


    }

}
